<template>
    <div class="e-table billList">
        <div class="top">
            <div class="left" style="margin-left: 10px">
                <div>
                    <el-button
                        size="mini"
                        @click="buttonAdd"
                        style="background: #7aa818; color: #fff"
                        v-permission="['edit']"
                        >新增</el-button
                    >
                </div>
            </div>
            <div class="right">
                <div class="right-div1">
                    <el-input
                        placeholder=""
                        v-model="query.keyword"
                        size="small"
                        @blur='getData'
                        @keyup.enter.native="getData"
                    >
                        <i
                            slot="suffix"
                            class="el-icon-search like"
                            @click="getData"
                        ></i>
                    </el-input>
                </div>
                <div>
                    <el-button
                        size="small"
                        style="background: #2e61d7; color: #fff"
                        @click="onAdvancedSearch"
                        >高级搜索</el-button
                    >
                </div>
            </div>
        </div>

        <el-table
            border
            ref="table"
            :data="tableData"
            style="width: 100%"
            class="table"
            highlight-current-rowrow
            height="0"
            v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'table' }"
        >
            <el-table-column
                label="结算编号"
                min-width="300"
                fixed
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span
                        @click="Examine(scope.row)"
                        style="color: blue; cursor: pointer"
                    >
                        {{ scope.row.billNo }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="billDate" label="结算日期" width="150" fixed>
            </el-table-column>

            <el-table-column
                prop="settleOrgName"
                label="结算单位"
                width="200"
                show-overflow-tooltip
                align="left"
            >
            </el-table-column>
            <el-table-column
                prop="totalTaxPrice"
                label="税价合计(元)"
                width="130"
                align="right"
            >
            </el-table-column>
            <el-table-column
                prop="amountExcludingTax"
                label="不含税金额（元）"
                min-width="150"
                align="right"
            >
            </el-table-column>
            <el-table-column
                prop="taxAmount"
                label="税额(元)"
                width="100"
                align="right"
            >
            </el-table-column>
            <el-table-column
                prop="cumulativeSettleAmount"
                label="累计结算金额(元)"
                width="150"
                align="right"
            >
            </el-table-column>
            <el-table-column prop="evaluate" label="动态评价" width="100">
                <template slot-scope="scope">
                    <span
                        @click="dynamicEvaluation(scope.row)"
                        style="color: blue; cursor: pointer"
                    >
                        {{
                            scope.row.evaluate ? scope.row.evaluate : '动态评价'
                        }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="状态" show-overflow-tooltip width="100">
                <template slot-scope="scope">
                    <ToolHistory
                        v-if="scope.row.state === 1"
                        :key="scope.$index"
                        url="/facilitysettle/flowEngin/get/audit/history"
                        :billId="scope.row.billId"
                        :billType="3021"
                    >
                        <span v-status="scope.row.state">
                            {{ showState(scope.row.state) }}
                        </span>
                    </ToolHistory>
                    <span v-status="scope.row.state" v-else>{{showState(scope.row.state)}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="redState" label="红单状态" width="100">
                <template v-slot="{ row }">
                    <span
                        style="color: blue; cursor: pointer"
                        @click="redClick(row)"
                        >{{ row.redState }}</span
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="orgName"
                label="机构"
                min-width="250"
                show-overflow-tooltip
                align="left"
            >
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <ComPagination
            :total="total"
            :pageSize.sync="query.limit"
            :currentPage.sync="query.page"
            @currentChange="getData"
            @sizeChange="getData"
            class="bottomSetting"
        />
    </div>
</template>
<script>
import ComPagination from '@/components/pagination/pagination.vue'
import request from '@/api/settlementManagement/equipmentBilling/contractOfCarriage'
import publics from '@/api/settlementManagement/equipmentBilling/public'
import {
    vueDom,
    debounce,
    openAppClient,
    advanceSearch,
    showLoading, hideLoading
} from '@/utils/common.js'
import { mapMutations } from 'vuex'

export default {
    components: {
        ComPagination
    },
    data () {
        return {
            tableData: [],
            query: {
                endTime: '',
                founderId: '',
                keyword: '',
                limit: 20,
                loginOrgId: '',
                orgId: '',
                orgName: '',
                page: 1,
                settleOrgId: '',
                settleOrgName: '',
                startTime: '',
                states: []
            },
            total: 1,
            tableHeight: 0,
            auditParams: {
                billId: '',
                billType: '3021',
                freeNextUnitIndex: null,
                freeNextUser: '',
                isPass: true,
                isShareInAudit: true,
                opinion: '',
                orgId: '',
                orgName: '',
                userId: '',
                workId: ''
            },
            evaluationBasis: {
                contractName: '', //合同名称
                contractNo: '', //合同编号
                outerId: '', //外包方id
                outerName: '', //外包方名称
                billId: '', //调用单据id
                billType: '3021', //	调用单据编号
                evaluatePeople: '', //评价人
                evaluatePeopleId: '', //评价人id
                orgId: '', //评价机构id
                orgName: '', //	评价机构名称
                settleNo: '', //结算编号
                useType: 0
            }
        }
    },
    computed: {
        //获取状态

    },
    created () {
        let data = JSON.parse(localStorage.getItem('vuex'))
        this.evaluationBasis.orgId = data.userInfo.orgInfo.orgId
        this.evaluationBasis.orgName = data.userInfo.orgInfo.orgName
        this.getData()
    },
    mounted () {
        // table动态高度
        vueDom(
            () => {
                this.tableHeight = document.body.clientHeight - 170
            },
            () => {
                return $('table').length !== 0
            }
        )
        this.fn = () => {
            this.tableHeight = document.body.clientHeight - 170
        }
        window.onresize = debounce(this.fn)
    },
    methods: {
        ...mapMutations(['setEvaluation', 'setSelectedInfo']),
        // 获取列表数据
        async getData () {
            try{
                showLoading()
                let res = await request.getContractOfCarriageData(this.query)
                let data = res.list
                data.forEach(item => {
                    switch (item.redBillState) {
                    case -1:
                        item.redState = ''
                        break
                    case 0:
                        item.redState = '冲销中'
                        break
                    case 1:
                        item.redState = '冲销中'
                        break
                    case 2:
                        item.redState = '已冲销'
                        break
                    case 3:
                        item.redState = '红单'
                        break
                    }
                })
                this.tableData = data
                this.total = res.totalCount
            }finally{
                hideLoading()
            }
        },
        //新增按钮
        buttonAdd () {
            this.$router.push('/contractOfCarriageSetAdd')
        },

        //点击编号进入编辑页/详情页
        Examine (row) {
            console.log(row)
            if (row.state === -1 || row.state === 2 || row.state === 5 || !this.permission(['edit'])) {
                this.$router.push({
                    path: '/contractOfCarriageSetDetail',
                    query: { billid: row.billId }
                })
            } else {
                this.auditParams.billId = row.billId
                publics.getCurrentUnitInfo(this.auditParams).then(res => {
                    if (res.canModify == true) {
                        this.$router.push({
                            path: '/contractOfCarriageSetEdit',
                            query: { billid: row.billId }
                        })
                    } else {
                        this.$router.push({
                            path: '/contractOfCarriageSetDetail',
                            query: { billid: row.billId }
                        })
                    }
                })
            }
        },
        redClick (row) {
            this.auditParams.billId = row.billId
            if (row.redBillState == 3) {
                // 如果该单据就是红单,则点击时进入源单详情页
                this.$router.push({
                    path: '/contractOfCarriageSetDetail',
                    query: { billid: row.sourceId }
                })
            } else {
                // 如果点击的该单据不是红单,则点击超链接进入红单
                // 如果进入红单,还需判断进入该红单的编辑页还是详情页
                if (row.redBillState == 0) {
                    this.$router.push({
                        path: '/contractOfCarriageSetEdit',
                        query: { billid: row.sourceId }
                    })
                } else if (row.redBillState == 1 || row.redBillState == 2) {
                    this.$router.push({
                        path: '/contractOfCarriageSetDetail',
                        query: { billid: row.sourceId }
                    })
                }
            }
        },
        // 动态评价
        dynamicEvaluation (row) {
            this.evaluationBasis.contractName = row.contractName
            this.evaluationBasis.contractNo = row.contractNo
            this.evaluationBasis.outerId = row.settleOrgId
            this.evaluationBasis.outerName = row.settleOrgName
            this.evaluationBasis.billId = row.billId
            this.evaluationBasis.evaluatePeople = row.founderName
            this.evaluationBasis.evaluatePeopleId = row.founderId
            this.evaluationBasis.settleNo = row.billNo
            this.setEvaluation(this.evaluationBasis)
            // this.setSelectedInfo(this.evaluationBasis)
            // openAppClient('/showSelectedData', '存入vuex里面的数据')
            openAppClient(
                '/dynamicEvaluation',
                '动态评价',
                () => {
                    this.getData()
                },
                null,
                null,
                'handleAdd'
            )
        },

        // 高级搜索
        onAdvancedSearch () {
            advanceSearch('/equipSettleAdvancedSearch').then(res => {
                this.setSelectedInfo(res)
                this.query.endTime = res.endTime
                this.query.keyword = res.keyword
                this.query.orgId = res.orgId
                this.query.orgName = res.orgName
                this.query.settleOrgId = res.settleOrgId
                this.query.settleOrgName = res.settleOrgName
                this.query.startTime = res.startTime
                this.query.states = res.states
                this.getData()
            })
        },
        showState (state) {
            const _state = this.$store.state.settlementManagement.ysState
            const obj = _state.find(x => x.value === Number(state))
            return obj.label
        }
    }
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed{
    height: 98.5% !important;
    bottom: 17px !important;
}
::v-deep .el-table__fixed-body-wrapper {
    height: 100% !important;
}
</style>
